<?php
/**
 * @title 1185. 一周中的第几天
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $day
     * @param Integer $month
     * @param Integer $year
     * @return String
     */
    function dayOfTheWeek($day, $month, $year) {
        /**
         * @since 2023-12-30 1971<=$year<=2100
         * @since 2023-12-30 1970-12-31是星期四
         * @since 2023-12-30 从1971到$year-1，一共有多少天
         */
        $dayCount = 0;
        if($year>1971){
            $yearCount = $year-1971;
            $dayCount += $yearCount*365;

            /**
             * @since 2023-12-30 计算从1971到$year-1，有多少个闰年
             * 能被4整除但不能被100整除的年份，或者能被400整除的年份
             * [1971, 2099]，不存在能被4整除，但不能被100整除的年份
             */
            if($yearCount >= 2){
                $dayCount += 1+ intval(($yearCount-2)/4);
            } else {}
        } else {}

        /**
         * @since 2023-12-30 $year是闰年还是非闰年
         */
        $monthArray = [31,28,31,30,31,30,31,31,30,31,30,31];
        if($year == 2100){
            /**
             * @since 2023-12-30 [1971, 2100]期间，只有2100，被4整除但不能被100整除，非闰年
             */
        } elseif($year % 4 == 0){
            /**
             * @since 2023-12-30 闰年
             */
            $monthArray[1] = 29;
        } else {}

        /**
         * @since 2023-12-30 计算1月到$month-1月，一共有多少天
         */
        for($m=0; $m<($month-1); $m++){
            $dayCount += $monthArray[$m];
        }
        $dayCount += $day;

        /**
         * @since 2023-12-30 计算$year-$month-$day是星期几
         */
        switch ($dayCount){
            case 1:
                $week = 5;
                break;
            case 2:
                $week = 6;
                break;
            case 3:
                $week = 0;
                break;
            default:
                $week = ($dayCount-3) % 7;
                break;
        }

        /**
         * @return String
         */
        $weekArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        return $weekArray[$week];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.30.php
 */
$datas = [
    [ 31, 8, 2019], // Saturday
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->dayOfTheWeek(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
